//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    ///<summary>
    /// Represents a pattern fill. A pattern fill is defined by two color and a type - typically solid.
    ///</summary>
    public class PatternFill : Fill
    {
        internal bool IsEmpty
        {
            get
            {
                if (this.Type == PatternType.None)
                    return true;
                if (BackgroundColor == null && ForegroundColor == null)
                    return true;
                return false;
            }
        }
        public PatternFill()
        {
            Type = PatternType.Solid;
        }
        public static PatternFill NewForegroundSolidFill(SpreadsheetColor color)
        {
            var result = new PatternFill { ForegroundColor = color };
            return result;
        }
        public static PatternFill NewBackgroundSolidFill(SpreadsheetColor color)
        {
            var result = new PatternFill { BackgroundColor = color, ForegroundColor = color};
            return result;
        }
        public PatternFill(SpreadsheetColor foregroundColor)
        {
            Type = PatternType.Solid;
            ForegroundColor = foregroundColor;
        }
        public PatternFill(SpreadsheetColor foregroundColor, SpreadsheetColor backgroundColor, PatternType type)
        {
            ForegroundColor = foregroundColor;
            BackgroundColor = backgroundColor;
            Type = type;
        }
        public PatternFill(PatternType type)
        {
            Type = type;
        }

        internal static SpreadsheetColor ReadOptionalColor(Workbook workbook, string name, ReadContext context)
        {
            if (context.ReaderLocalName == name)
            {
                return SpreadsheetColor.ReadColor(name, context);
                //ColorReference c = new ColorReference();
                //c.ReadXml(name, reader);
                //return c;
            }
            return null;
        }
#if DEBUG
        public override string ToString()
        {
            return string.Format("PatternFill {0} {1} {2}", this.Type,  _bgColor, ForegroundColor);
        }
#endif
        private SpreadsheetColor _fgColor;

        internal SpreadsheetColor GetColor()
        {
            if (ForegroundColor != null)
                return ForegroundColor;
            return BackgroundColor;
        }
        public SpreadsheetColor ForegroundColor
        {
            get { return _fgColor; }
            private set
            {
                _fgColor = value;
            }
        }
        private SpreadsheetColor _bgColor;

        public SpreadsheetColor BackgroundColor
        {
            get { return _bgColor; }
            private set { _bgColor = value; }
        }

        public override int GetHashCode()
        {
            //\\int result = Type.GetHashCode();
            int result = (int) this.Type;
            if (ForegroundColor != null)
                result += ForegroundColor.GetHashCode();
            if (BackgroundColor != null)
                result += BackgroundColor.GetHashCode();
            return result;
        }

        public override bool Equals(Fill fill)
        {
            var patternFill = fill as PatternFill;
            if (patternFill == null)
                return this.ForegroundColor == null && this.BackgroundColor == null;
            return patternFill.Type == Type && Equals(ForegroundColor, patternFill.ForegroundColor) && Equals(BackgroundColor, patternFill.BackgroundColor);
        }

        internal override Fill Clone()
        {
            var result = new PatternFill(ForegroundColor, BackgroundColor, Type)
                            /* {
                                 BackgroundColor = BackgroundColor,
                                 ForegroundColor = ForegroundColor,
                                 Type = Type
                             }*/;
            return result;
        }


        internal override void ReadXmlCore(ReadContext context)
        {
            using (ReadContext c = context.Read("patternFill"))
            {
                var type = c.GetOptionalEnum<PatternType>("patternType");
                Type = type.GetValueOrDefault(PatternType.None);
                var wb = c.GetData<Workbook>();
                ForegroundColor = ReadOptionalColor(wb, "fgColor", c);
                BackgroundColor = ReadOptionalColor(wb, "bgColor", c);
                if (type == null && BackgroundColor != null)
                {
                    Type = PatternType.Solid;
                    if (ForegroundColor == null)
                        ForegroundColor = BackgroundColor;
                }
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("fill"))
            {
                using (WriteContext cc = c.Write("patternFill"))
                {
                    //if ((BackgroundColor == null || ForegroundColor != null) || Type != PatternType.Solid)
                    cc.SetEnum("patternType", Type);
                    if (ForegroundColor != null)
                        ForegroundColor.WriteXmlAs("fgColor", cc);
                    if (BackgroundColor != null)
                        BackgroundColor.WriteXmlAs("bgColor", cc);
                }
            }
        }
    }
}
